package com.eipc;

import org.androidpn.sdk.MainService;
import org.androidpn.sdk.ServiceManager;

import com.eipc.info.R;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class EipcMainService extends Service {
	private static ServiceManager mServiceManager;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		mServiceManager = new ServiceManager(this);

		new Thread(new Runnable() {
			@Override
			public void run() {
				Log.e("-----------startFetchPerson-----------------",
						"startFetchPerson");
				startFetchPerson();
			}
		}).start();

//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				Log.e("-----------startFetchData-----------------",
//						"startFetchData");
//				startFetchNotice();
//			}
//		}).start();

//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				Log.e("-----------startFushLogin-----------------",
//						"startFushLogin");
//				startReloginPush();
//			}
//		}).start();

//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				Log.e("-----------startFetchReply-----------------",
//						"startFetchReply");
//				startFetchReply();
//			}
//		}).start();
	}

	@Override
	public void onStart(Intent intent, int startId) {

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
//		stopFetchNotice();
//		stopReloginPush();
//		stopFetchReply();

		stopFetchPerson();

		Log.e("-----------stop eipc main service-----------------",
				"startFushLogin");
	}

	private void startFetchPerson() {
		Intent intent = new Intent(this, FetchPersonService.class);
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager) this
				.getSystemService(Activity.ALARM_SERVICE);
		am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
				24 * 60 * 60 * 1000, sender);
	}

	private void stopFetchPerson() {
		Intent intent = new Intent(this, FetchPersonService.class);
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager) this
				.getSystemService(Activity.ALARM_SERVICE);
		am.cancel(sender);
	}

	private void startFetchNotice() {
		Intent intent = new Intent(this, FetchNoticeService.class);
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager) this
				.getSystemService(Activity.ALARM_SERVICE);
		am
				.setRepeating(AlarmManager.RTC,
						System.currentTimeMillis() + 6000,
						this.getResources().getInteger(
								R.integer.fetch_data_interval) * 60 * 1000,
						sender);
	}

	private void startFetchReply() {
		Intent intent = new Intent(this, FetchReplyService1.class);
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager) this
				.getSystemService(Activity.ALARM_SERVICE);
		am.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + 90000,
				30000, sender);
	}

	private void startReloginPush() {
		Intent intent = MainService.getIntent();
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager) this
				.getSystemService(Activity.ALARM_SERVICE);
		am.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + 500,
				60 * 1000 * 10, sender);
	}

	private void stopFetchReply() {
		Intent intent = new Intent(this, FetchReplyService1.class);
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager) this
				.getSystemService(Activity.ALARM_SERVICE);
		am.cancel(sender);
	}

	private void stopFetchNotice() {
		Intent intent = new Intent(this, FetchNoticeService.class);
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager) this
				.getSystemService(Activity.ALARM_SERVICE);
		am.cancel(sender);
	}

	private void stopReloginPush() {
		Intent intent = MainService.getIntent();
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager) this
				.getSystemService(Activity.ALARM_SERVICE);
		am.cancel(sender);
	}
}
